<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="字符串" tabindex="-1"><a class="header-anchor" href="#字符串"><span>字符串</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a></li>
<li><a href="#available-methods">可用的方法</a></li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p>Laravel 包含多种用于操作字符串值的函数。其中许多函数是框架本身也在使用的，如果你觉得方便，也可以在自己的应用程序中使用它们。</p>
<h2 id="可用的方法" tabindex="-1"><a class="header-anchor" href="#可用的方法"><span>可用的方法</span></a></h2>
<h3 id="字符串-1" tabindex="-1"><a class="header-anchor" href="#字符串-1"><span>字符串</span></a></h3>
<h3 id="字符流处理" tabindex="-1"><a class="header-anchor" href="#字符流处理"><span>字符流处理</span></a></h3>
<h2 id="字符串-2" tabindex="-1"><a class="header-anchor" href="#字符串-2"><span>字符串</span></a></h2>
<h4 id="" tabindex="-1"><a class="header-anchor" href="#"><span><code v-pre>__()</code></span></a></h4>
<p><code v-pre>__</code> 函数使用<a href="https://learnku.com/docs/laravel/11.x/localization" title="本地化文件" target="_blank" rel="noopener noreferrer">本地化文件</a>来翻译指定的字符串或 Key ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Welcome to our application'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">echo</span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.welcome'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果给定翻译的字符串或者 key 不存在， 则 <strong>会返回你指定的值。所以上述例子中， 如果给定翻译的字符串或者 key 不存在，则</strong> 函数会返回 messages.welcome</p>
<h4 id="class-basename" tabindex="-1"><a class="header-anchor" href="#class-basename"><span><code v-pre>class_basename()</code></span></a></h4>
<p><code v-pre>class_basename</code> 函数返回不带命名空间的特定类的类名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$class</span> <span class="token operator">=</span> <span class="token function">class_basename</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Foo\Bar\Baz'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Baz</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="e" tabindex="-1"><a class="header-anchor" href="#e"><span><code v-pre>e()</code></span></a></h4>
<p><code v-pre>e</code> 函数运行 PHP 的 <code v-pre>htmlspecialchars</code> 函数，且 <code v-pre>double_encode</code> 默认设定为 <code v-pre>true</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">e</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'&lt;html>foo&lt;/html>'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// &amp;lt;html&amp;gt;foo&amp;lt;/html&amp;gt;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="preg-replace-array" tabindex="-1"><a class="header-anchor" href="#preg-replace-array"><span><code v-pre>preg_replace_array()</code></span></a></h4>
<p><code v-pre>preg_replace_array</code> 函数按数组顺序替换字符串中符合给定模式的字符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'The event will take place between :start and :end'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token function">preg_replace_array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/:[a-z_]+/'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'8:30'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'9:00'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token variable">$string</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// The event will take place between 8:30 and 9:00</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-after" tabindex="-1"><a class="header-anchor" href="#str-after"><span><code v-pre>Str::after()</code></span></a></h4>
<p><code v-pre>Str::after</code> 方法返回字符串中指定值之后的所有内容。如果字符串中不存在这个值，它将返回整个字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">after</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'This is'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ' my name'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-afterlast" tabindex="-1"><a class="header-anchor" href="#str-afterlast"><span><code v-pre>Str::afterLast()</code></span></a></h4>
<p><code v-pre>Str::afterLast</code> 方法返回字符串中指定值最后一次出现后的所有内容。如果字符串中不存在这个值，它将返回整个字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">afterLast</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'App\Http\Controllers\Controller'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'\\'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'Controller'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-apa" tabindex="-1"><a class="header-anchor" href="#str-apa"><span><code v-pre>Str::apa()</code></span></a></h4>
<p><code v-pre>Str::apa</code> 方法将给定的字符串转换成符合 <a href="https://apastyle.apa.org/style-grammar-guidelines/capitalization/title-case" title="APA 格式指南" target="_blank" rel="noopener noreferrer">APA 格式指南</a>大小写规则的标题格式：<br>
The <code v-pre>Str::apa</code> method converts the given string to title case following the <a href="https://apastyle.apa.org/style-grammar-guidelines/capitalization/title-case" target="_blank" rel="noopener noreferrer">APA guidelines</a>:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$title</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">apa</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Creating A Project'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'Creating a Project'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-ascii" tabindex="-1"><a class="header-anchor" href="#str-ascii"><span><code v-pre>Str::ascii()</code></span></a></h4>
<p><code v-pre>Str::ascii</code> 方法会尝试将字符串转换为 ASCII 值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">ascii</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'û'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'u'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-before" tabindex="-1"><a class="header-anchor" href="#str-before"><span><code v-pre>Str::before()</code></span></a></h4>
<p><code v-pre>Str::before</code> 方法返回字符串中指定值之前的所有内容：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">before</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'my name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'This is '</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-beforelast" tabindex="-1"><a class="header-anchor" href="#str-beforelast"><span><code v-pre>Str::beforeLast()</code></span></a></h4>
<p><code v-pre>Str::beforeLast</code> 方法返回字符串中指定值最后一次出现前的所有内容：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">beforeLast</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'is'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'This '</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-between" tabindex="-1"><a class="header-anchor" href="#str-between"><span><code v-pre>Str::between()</code></span></a></h4>
<p><code v-pre>Str::between</code> 方法返回字符串在指定两个值之间的内容：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">between</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'This'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ' is my '</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-betweenfirst" tabindex="-1"><a class="header-anchor" href="#str-betweenfirst"><span><code v-pre>Str::betweenFirst()</code></span></a></h4>
<p><code v-pre>Str::betweenFirst</code> 方法返回字符串在指定两个值之间的最小可能的部分：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">betweenFirst</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'[a] bc [d]'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'['</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">']'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'a'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-camel" tabindex="-1"><a class="header-anchor" href="#str-camel"><span><code v-pre>Str::camel()</code></span></a></h4>
<p><code v-pre>Str::camel</code> 方法将指定字符串转换为 <code v-pre>驼峰式</code> 表示方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">camel</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo_bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'fooBar'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-charat" tabindex="-1"><a class="header-anchor" href="#str-charat"><span><code v-pre>Str::charAt()</code></span></a></h4>
<p><code v-pre>Str::charAt</code> 方法返回指定索引处的字符。如果索引超出范围，则返回 <code v-pre>false</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$character</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">charAt</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name.'</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 's'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-contains" tabindex="-1"><a class="header-anchor" href="#str-contains"><span><code v-pre>Str::contains()</code></span></a></h4>
<p><code v-pre>Str::contains</code> 方法用来确定给定的字符串中是否包含某个值。该方法大小写敏感：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$contains</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'my'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以传入一个值数组，用以确定给定的字符串中是否包含该数组中的任何值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$contains</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'my'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-containsall" tabindex="-1"><a class="header-anchor" href="#str-containsall"><span><code v-pre>Str::containsAll()</code></span></a></h4>
<p><code v-pre>Str::containsAll</code> 用以确定给定的字符串是否包含给定数组中的所有值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$containsAll</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">containsAll</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'my'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-endswith" tabindex="-1"><a class="header-anchor" href="#str-endswith"><span><code v-pre>Str::endsWith()</code></span></a></h4>
<p><code v-pre>Str::endsWith</code> 方法用来确定给的给定的字符串是否以某个特定值结尾：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">endsWith</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以传递一个值数组，用以确定给定字符串在是否以该数组中的某个值作为结尾：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">endsWith</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">endsWith</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'this'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-excerpt" tabindex="-1"><a class="header-anchor" href="#str-excerpt"><span><code v-pre>Str::excerpt()</code></span></a></h4>
<p><code v-pre>Str::excerpt</code> 提取给定字符串中与该字符串中短语的第一个实例匹配的摘要：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$excerpt</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">excerpt</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'my'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'radius'</span> <span class="token operator">=></span> <span class="token number">3</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// '...is my na...'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>radius</code> 选项(默认是 <code v-pre>100</code>)允许你定义截断字符串两边的字符数。</p>
<p>此外，你还可以使用 <code v-pre>omission</code> 选项来定义要在次该截断字符串中前后附加的字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$excerpt</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">excerpt</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'radius'</span> <span class="token operator">=></span> <span class="token number">3</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'omission'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'(...) '</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// '(...) my name'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-finish" tabindex="-1"><a class="header-anchor" href="#str-finish"><span><code v-pre>Str::finish()</code></span></a></h4>
<p>如果给定字符串尚未以某个值结尾，<code v-pre>Str::finish</code> 方法用来将该值的单个实例添加到字符串中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$adjusted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">finish</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'this/string'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// this/string/</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$adjusted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">finish</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'this/string/'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// this/string/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-headline" tabindex="-1"><a class="header-anchor" href="#str-headline"><span><code v-pre>Str::headline()</code></span></a></h4>
<p><code v-pre>Str::headline</code> 方法将由大小写、连字符或下划线分隔的字符串转换为空格分隔的字符串，每个单词的第一个字母大写：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$headline</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">headline</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'steve_jobs'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Steve Jobs</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$headline</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">headline</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'EmailNotificationSent'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Email Notification Sent</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-inlinemarkdown" tabindex="-1"><a class="header-anchor" href="#str-inlinemarkdown"><span><code v-pre>Str::inlineMarkdown()</code></span></a></h4>
<p><code v-pre>Str::inlineMarkdown</code> 使用 <a href="https://commonmark.thephpleague.com/" target="_blank" rel="noopener noreferrer">CommonMark</a> 将 GitHub 风格的 Markdown 转换成行内 HTML。不过，不同于 <code v-pre>markdown</code> 方法，它不会将所有生成的 HTML 包装在一个块级别元素中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$html</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">inlineMarkdown</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'**Laravel**'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// &lt;strong>Laravel&lt;/strong></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="markdown-安全" tabindex="-1"><a class="header-anchor" href="#markdown-安全"><span>Markdown 安全</span></a></h4>
<p>默认情况下，Markdow 支持原生 HTML，当与原始用户输入一起使用时，它将暴露跨站点脚本(XSS)漏洞。根据 <a href="https://commonmark.thephpleague.com/security/" target="_blank" rel="noopener noreferrer">CommonMark 安全文档</a>，你可以使用 <code v-pre>html_input</code> 选项转义或者剥离原始 HTML，并使用 <code v-pre>allow_unsafe_links</code> 选项来之定义是否允许不安全的链接，如果你需要允许用户使用一些原始 HTML，你应该通过 HTML Purifier 传递编译过的 Markdown：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">inlineMarkdown</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Inject: &lt;script>alert("Hello XSS!");&lt;/script>'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'html_input'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'strip'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'allow_unsafe_links'</span> <span class="token operator">=></span> <span class="token constant boolean">false</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Inject: alert(&amp;quot;Hello XSS!&amp;quot;);</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-is" tabindex="-1"><a class="header-anchor" href="#str-is"><span><code v-pre>Str::is()</code></span></a></h4>
<p><code v-pre>Str::is</code> 方法用来确定给定的字符串是否匹配给定模式。星号可用作通配符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$matches</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">is</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo*'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foobar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$matches</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">is</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'baz*'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foobar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-isascii" tabindex="-1"><a class="header-anchor" href="#str-isascii"><span><code v-pre>Str::isAscii()</code></span></a></h4>
<p><code v-pre>Str::isAscii</code> 方法用来确定各给定的字符串是否为 7 位 ASCII 码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isAscii</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">isAscii</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isAscii</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">isAscii</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'ü'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-isjson" tabindex="-1"><a class="header-anchor" href="#str-isjson"><span><code v-pre>Str::isJson()</code></span></a></h4>
<p><code v-pre>Str::isJson</code> 用来确定给定字符串是否是有效的 JSON 值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">isJson</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'[1,2,3]'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">isJson</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'{"first": "John", "last": "Doe"}'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">isJson</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'{first: "John", last: "Doe"}'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-isurl" tabindex="-1"><a class="header-anchor" href="#str-isurl"><span><code v-pre>Str::isUrl()</code></span></a></h4>
<p><code v-pre>Str::isUrl</code> 用于确定给定字符串是否是有效的 URL：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isUrl</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">isUrl</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://example.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isUrl</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">isUrl</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'laravel'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>isUrl</code> 方法认将宽泛意义上的协议视为有效。不过，你可以将你认为有效的协议传递给 <code v-pre>isUrl</code> 方法来指定协议：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$isUrl</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">isUrl</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://example.com'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'http'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'https'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="str-isulid" tabindex="-1"><a class="header-anchor" href="#str-isulid"><span><code v-pre>Str::isUlid()</code></span></a></h4>
<p><code v-pre>Str::isUlid</code> 方法用来确定指定的字符串是否是有效的 ULID：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isUlid</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">isUlid</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'01gd6r360bp37zj17nxb55yv40'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isUlid</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">isUlid</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'laravel'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-isuuid" tabindex="-1"><a class="header-anchor" href="#str-isuuid"><span><code v-pre>Str::isUuid()</code></span></a></h4>
<p><code v-pre>Str::isUuid</code> 方法用来确定指定的字符串是否是有效的 UUID：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isUuid</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">isUuid</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'a0a2a2d2-0b87-4a18-83f2-2529882be2de'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isUuid</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">isUuid</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'laravel'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-kebab" tabindex="-1"><a class="header-anchor" href="#str-kebab"><span><code v-pre>Str::kebab()</code></span></a></h4>
<p><code v-pre>Str::kebab</code> 方法将给定字符串转换位中横线命名法(<code v-pre>kebab-case</code>)：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">kebab</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'fooBar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// foo-bar</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-lcfirst" tabindex="-1"><a class="header-anchor" href="#str-lcfirst"><span><code v-pre>Str::lcfirst()</code></span></a></h4>
<p><code v-pre>Str::lcfirst</code> 方法以首字母小写的方式返回给定的字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">lcfirst</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Foo Bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// foo Bar</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-length" tabindex="-1"><a class="header-anchor" href="#str-length"><span><code v-pre>Str::length()</code></span></a></h4>
<p><code v-pre>Str::length</code> 返回给定字符串的长度：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$length</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 7</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-limit" tabindex="-1"><a class="header-anchor" href="#str-limit"><span><code v-pre>Str::limit()</code></span></a></h4>
<p><code v-pre>Str::limit</code> 方法将给定字符串截断为指定长度：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$truncated</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'The quick brown fox jumps over the lazy dog'</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// The quick brown fox...</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以将第三个参数传入到该方法，以修改截断后的字符串末尾要追加的字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$truncated</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'The quick brown fox jumps over the lazy dog'</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">' (...)'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// The quick brown fox (...)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-lower" tabindex="-1"><a class="header-anchor" href="#str-lower"><span><code v-pre>Str::lower()</code></span></a></h4>
<p><code v-pre>Str::lower</code> 方法将字符串转换成小写字母：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">lower</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'LARAVEL'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// laravel</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-markdown" tabindex="-1"><a class="header-anchor" href="#str-markdown"><span><code v-pre>Str::markdown()</code></span></a></h4>
<p><code v-pre>Str::markdown</code> 方法使用 <a href="https://commonmark.thephpleague.com/" target="_blank" rel="noopener noreferrer">CommonMark</a> 将 Github 风格的 Markdown 转换成 HTML：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$html</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">markdown</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'# Laravel'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// &lt;h1>Laravel&lt;/h1></span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$html</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">markdown</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'# Taylor &lt;b>Otwell&lt;/b>'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'html_input'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'strip'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// &lt;h1>Taylor Otwell&lt;/h1></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="markdown-安全-1" tabindex="-1"><a class="header-anchor" href="#markdown-安全-1"><span>Markdown 安全</span></a></h4>
<p>默认情况下，Markdown 支持原始 HTML，这当使用原始用户输入时，这可能会暴露跨站脚本(XSS)漏洞。根据 <a href="https://commonmark.thephpleague.com/security/" target="_blank" rel="noopener noreferrer">CommonMark 安全文档</a>，你可以使用 <code v-pre>html_input</code> 选项来转义或者剥离原始 HTML，也可以使用 <code v-pre>allow_unsafe_links</code> 选项来指定是否允许不安全链接。如果你想允许原始 HTML，你应该通过 HTML Purifier 传递编译后的 Markdown：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">markdown</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Inject: &lt;script>alert("Hello XSS!");&lt;/script>'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'html_input'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'strip'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'allow_unsafe_links'</span> <span class="token operator">=></span> <span class="token constant boolean">false</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// &lt;p>Inject: alert(&amp;quot;Hello XSS!&amp;quot;);&lt;/p></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-mask" tabindex="-1"><a class="header-anchor" href="#str-mask"><span><code v-pre>Str::mask()</code></span></a></h4>
<p><code v-pre>Str::mask</code> 方法用重复字符掩盖字符串的一部分，可用于混淆电子邮件地址和电话号码等字符串片段：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">mask</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taylor@example.com'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'*'</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// tay***************</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果需要，您可以提供一个负数作为该 <code v-pre>mask</code> 方法的第三个参数，这将指示该方法从距字符串末尾的给定距离开始进行遮蔽：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">mask</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taylor@example.com'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'*'</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">15</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// tay***@example.com</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-ordereduuid" tabindex="-1"><a class="header-anchor" href="#str-ordereduuid"><span><code v-pre>Str::orderedUuid()</code></span></a></h4>
<p><code v-pre>Str::orderedUuid</code> 方法生成「时间戳优先」的 UUID，可高效地存储在索引数据库列中。使用此方法生成的每个 UUID 将排在先前使用该方法生成的 UUID 之后：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword type-casting">string</span><span class="token punctuation">)</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">orderedUuid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-padboth" tabindex="-1"><a class="header-anchor" href="#str-padboth"><span><code v-pre>Str::padBoth()</code></span></a></h4>
<p><code v-pre>Str::padBoth</code> 方法包装了 PHP 的 <code v-pre>str_pad</code> 函数，用另一个字符串填充字符串的两侧，直到最终字符串达到所需长度：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$padded</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">padBoth</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'James'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'_'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// '__James___'</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$padded</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">padBoth</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'James'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// '  James   '</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-padleft" tabindex="-1"><a class="header-anchor" href="#str-padleft"><span><code v-pre>Str::padLeft()</code></span></a></h4>
<p><code v-pre>Str::padLeft</code> 方法包装了 PHP 的 <code v-pre>str_pad</code> 函数，用另一个字符串填充字符串的左侧，直到最终字符串达到所需长度：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$padded</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">padLeft</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'James'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'-='</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// '-=-=-James'</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$padded</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">padLeft</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'James'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// '     James'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-padright" tabindex="-1"><a class="header-anchor" href="#str-padright"><span><code v-pre>Str::padRight()</code></span></a></h4>
<p><code v-pre>Str::padRight</code> 方法包装了 PHP 的 <code v-pre>str_pad</code> 函数，用另一个字符串填充字符串的右侧，直到最终字符串达到所需长度：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$padded</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">padRight</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'James'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'-'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'James-----'</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$padded</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">padRight</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'James'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'James     '</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-password" tabindex="-1"><a class="header-anchor" href="#str-password"><span><code v-pre>Str::password()</code></span></a></h4>
<p><code v-pre>Str::password</code> 方法可用于生成给定长度的安全随机密码。密码将由字母、数字、符号和空格的组合组成。默认情况下，密码长度为 32 个字符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$password</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">password</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'EbJo2vE-AS:U,$%_gkrV4n,q~1xy/-_4'</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$password</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">password</span><span class="token punctuation">(</span><span class="token number">12</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'qwuar>#V|i]N'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-plural" tabindex="-1"><a class="header-anchor" href="#str-plural"><span><code v-pre>Str::plural()</code></span></a></h4>
<p><code v-pre>Str::plural</code> 方法将单数字符串转换为复数形式。此函数支持 <a href="https://learnku.com/docs/laravel/11.x/localizationmd#pluralization-language" target="_blank" rel="noopener noreferrer">Laravel 复数转换器支持的任何语言</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">plural</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'car'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// cars</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">plural</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'child'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// children</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>您可以向函数提供一个整数作为第二个参数来检索字符串的单数或复数形式：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">plural</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'child'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// children</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$singular</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">plural</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'child'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// child</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-pluralstudly" tabindex="-1"><a class="header-anchor" href="#str-pluralstudly"><span><code v-pre>Str::pluralStudly()</code></span></a></h4>
<p><code v-pre>Str::pluralStudly</code> 方法将以大写字母格式的单数字符串转换为复数形式。此函数支持 <a href="https://learnku.com/docs/laravel/11.x/localizationmd#pluralization-language" target="_blank" rel="noopener noreferrer">Laravel 复数转换器支持的任何语言</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">pluralStudly</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'VerifiedHuman'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// VerifiedHumans</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">pluralStudly</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'UserFeedback'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// UserFeedback</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>您可以向函数提供一个整数作为第二个参数来检索字符串的单数或复数形式：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">pluralStudly</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'VerifiedHuman'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// VerifiedHumans</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$singular</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">pluralStudly</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'VerifiedHuman'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// VerifiedHuman</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-position" tabindex="-1"><a class="header-anchor" href="#str-position"><span><code v-pre>Str::position()</code></span></a></h4>
<p><code v-pre>Str::position</code> 方法返回字符串中子字符串第一次出现的位置。如果给定字符串中不存在子字符串，则 <code v-pre>false</code> 返回：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$position</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">position</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello, World!'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Hello'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 0</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$position</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">position</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello, World!'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'W'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 7</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-random" tabindex="-1"><a class="header-anchor" href="#str-random"><span><code v-pre>Str::random()</code></span></a></h4>
<p><code v-pre>Str::random</code> 方法生成指定长度的随机字符串。该函数使用PHP的 <code v-pre>random_bytes</code> 函数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$random</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token number">40</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在测试期间，「伪造」<code v-pre>Str::random</code> 方法返回的值可能会很有用。为此，您可以使用 <code v-pre>createRandomStringsUsing</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">createRandomStringsUsing</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token string single-quoted-string">'fake-random-string'</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要指示 <code v-pre>random</code> 方法返回正常生成随机字符串，您可以调用 <code v-pre>createRandomStringsNormally</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">createRandomStringsNormally</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="str-remove" tabindex="-1"><a class="header-anchor" href="#str-remove"><span><code v-pre>Str::remove()</code></span></a></h4>
<p><code v-pre>Str::remove</code> 方法从字符串中删除给定的值或值数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'Peter Piper picked a peck of pickled peppers.'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$removed</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'e'</span><span class="token punctuation">,</span> <span class="token variable">$string</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Ptr Pipr pickd a pck of pickld ppprs.</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>您还可以将 <code v-pre>false</code> 第三个参数传递给该 <code v-pre>remove</code> 方法，以便在删除字符串时忽略大小写。</p>
<h4 id="str-repeat" tabindex="-1"><a class="header-anchor" href="#str-repeat"><span><code v-pre>Str::repeat()</code></span></a></h4>
<p><code v-pre>Str::repeat</code> 方法重复给定的字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'a'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$repeat</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">repeat</span><span class="token punctuation">(</span><span class="token variable">$string</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// aaaaa</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-replace" tabindex="-1"><a class="header-anchor" href="#str-replace"><span><code v-pre>Str::replace()</code></span></a></h4>
<p><code v-pre>Str::replace</code> 方法使用字符串替换字符串中的给定字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'Laravel 10.x'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'10.x'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'11.x'</span><span class="token punctuation">,</span> <span class="token variable">$string</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Laravel 11.x</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>replace</code>方法还接受一个 <code v-pre>caseSensitive</code> 参数。默认情况下，该 <code v-pre>replace</code> 方法区分大小写：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Framework'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">,</span> <span class="token argument-name">caseSensitive</span><span class="token punctuation">:</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="str-replacearray" tabindex="-1"><a class="header-anchor" href="#str-replacearray"><span><code v-pre>Str::replaceArray()</code></span></a></h4>
<p><code v-pre>Str::replaceArray</code> 方法使用数组按顺序替换字符串中的给定值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'The event will take place between ? and ?'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">replaceArray</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'?'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'8:30'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'9:00'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token variable">$string</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// The event will take place between 8:30 and 9:00</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-replacefirst" tabindex="-1"><a class="header-anchor" href="#str-replacefirst"><span><code v-pre>Str::replaceFirst()</code></span></a></h4>
<p><code v-pre>Str::replaceFirst</code> 方法替换字符串中第一次出现的给定值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">replaceFirst</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'the'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'a'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'the quick brown fox jumps over the lazy dog'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// a quick brown fox jumps over the lazy dog</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-replacelast" tabindex="-1"><a class="header-anchor" href="#str-replacelast"><span><code v-pre>Str::replaceLast()</code></span></a></h4>
<p><code v-pre>Str::replaceLast</code> 方法替换字符串中最后一次出现的给定值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">replaceLast</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'the'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'a'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'the quick brown fox jumps over the lazy dog'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// the quick brown fox jumps over a lazy dog</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-replacematches" tabindex="-1"><a class="header-anchor" href="#str-replacematches"><span><code v-pre>Str::replaceMatches()</code></span></a></h4>
<p><code v-pre>Str::replaceMatches</code> 方法用给定的替换字符串替换与模式匹配的字符串的所有部分：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">replaceMatches</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token argument-name">pattern</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'/[^A-Za-z0-9]++/'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token argument-name">replace</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">''</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token argument-name">subject</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'(+1) 501-555-1000'</span></span>
<span class="line">    <span class="token punctuation">)</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// '15015551000'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>replaceMatches</code> 方法还接受一个闭包，该闭包将使用与给定模式匹配的字符串的每个部分进行调用，从而允许您在闭包内执行替换逻辑并返回替换的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">replaceMatches</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/\d/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">array</span> <span class="token variable">$matches</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token string single-quoted-string">'['</span><span class="token operator">.</span><span class="token variable">$matches</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token operator">.</span><span class="token string single-quoted-string">']'</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'123'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// '[1][2][3]'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-replacestart" tabindex="-1"><a class="header-anchor" href="#str-replacestart"><span><code v-pre>Str::replaceStart()</code></span></a></h4>
<p><code v-pre>Str::replaceStart</code> 仅当给定值出现在字符串的开头时，该方法才会替换该值的第一次出现：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">replaceStart</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Laravel World</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">replaceStart</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'World'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Hello World</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-replaceend" tabindex="-1"><a class="header-anchor" href="#str-replaceend"><span><code v-pre>Str::replaceEnd()</code></span></a></h4>
<p><code v-pre>Str::replaceEnd</code> 仅当给定值出现在字符串末尾时，该方法才会替换该值的最后一次出现：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">replaceEnd</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'World'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Hello Laravel</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">replaceEnd</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Hello World</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-reverse" tabindex="-1"><a class="header-anchor" href="#str-reverse"><span><code v-pre>Str::reverse()</code></span></a></h4>
<p><code v-pre>Str::reverse</code> 方法反转给定的字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$reversed</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">reverse</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// dlroW olleH</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-singular" tabindex="-1"><a class="header-anchor" href="#str-singular"><span><code v-pre>Str::singular()</code></span></a></h4>
<p><code v-pre>Str::singular</code> 方法将字符串转换为单数形式。此函数支持 <a href="https://learnku.com/docs/laravel/11.x/localizationmd#pluralization-language" target="_blank" rel="noopener noreferrer">Laravel 复数转换器支持的任何语言</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$singular</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">singular</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'cars'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// car</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$singular</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">singular</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'children'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// child</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-slug" tabindex="-1"><a class="header-anchor" href="#str-slug"><span><code v-pre>Str::slug()</code></span></a></h4>
<p><code v-pre>Str::slug</code> 方法根据给定的字符串生成 URL 友好的「slug」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$slug</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">slug</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel 5 Framework'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'-'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// laravel-5-framework</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-snake" tabindex="-1"><a class="header-anchor" href="#str-snake"><span><code v-pre>Str::snake()</code></span></a></h4>
<p><code v-pre>Str::snake</code> 方法将给定的字符串转换为 <code v-pre>snake_case</code> ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">snake</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'fooBar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// foo_bar</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">snake</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'fooBar'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'-'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// foo-bar</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-squish" tabindex="-1"><a class="header-anchor" href="#str-squish"><span><code v-pre>Str::squish()</code></span></a></h4>
<p><code v-pre>Str::squish</code> 方法从字符串中删除所有多余的空格，包括单词之间的多余的空格：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">squish</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'    laravel    framework    '</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// laravel framework</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-start" tabindex="-1"><a class="header-anchor" href="#str-start"><span><code v-pre>Str::start()</code></span></a></h4>
<p>如果字符串尚未以该值开头，则 <code v-pre>Str::start</code> 方法将给定值的单个实例添加到字符串中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$adjusted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'this/string'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// /this/string</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$adjusted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/this/string'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// /this/string</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-startswith" tabindex="-1"><a class="header-anchor" href="#str-startswith"><span><code v-pre>Str::startsWith()</code></span></a></h4>
<p><code v-pre>Str::startsWith</code> 方法检测给定的字符串是否以给定的值开头：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">startsWith</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'This'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果传递了一个可能值数组，则当字符串以数组中任意给定值开头时 <code v-pre>startsWith</code> 方法将返回 <code v-pre>true</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">startsWith</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'This'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'That'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'There'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-studly" tabindex="-1"><a class="header-anchor" href="#str-studly"><span><code v-pre>Str::studly()</code></span></a></h4>
<p><code v-pre>Str::studly</code> 方法将给定的字符串转换为 <code v-pre>StudlyCase</code> ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">studly</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo_bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// FooBar</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-substr" tabindex="-1"><a class="header-anchor" href="#str-substr"><span><code v-pre>Str::substr()</code></span></a></h4>
<p><code v-pre>Str::substr</code> 方法返回由开始位置和长度参数指定的字符串部分：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'The Laravel Framework'</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Laravel</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-substrcount" tabindex="-1"><a class="header-anchor" href="#str-substrcount"><span><code v-pre>Str::substrCount()</code></span></a></h4>
<p><code v-pre>Str::substrCount</code> 方法返回给定字符串中给定值出现的次数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$count</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">substrCount</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'If you like ice cream, you will like snow cones.'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'like'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 2</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-substrreplace" tabindex="-1"><a class="header-anchor" href="#str-substrreplace"><span><code v-pre>Str::substrReplace()</code></span></a></h4>
<p><code v-pre>Str::substrReplace</code> 方法替换字符串中某一部分内的文本，从第三个参数指定的位置开始，替换第四个参数指定的字符数。传递 <code v-pre>0</code> 给该方法的第四个参数将在指定位置插入字符串，但不替换字符串中的任何现有字符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">substrReplace</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'1300'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">':'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token comment">// 13:</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">substrReplace</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'1300'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">':'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token comment">// 13:00</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-swap" tabindex="-1"><a class="header-anchor" href="#str-swap"><span><code v-pre>Str::swap()</code></span></a></h4>
<p><code v-pre>Str::swap</code> 方法使用 PHP 的函数 <code v-pre>strtr</code> 替换给定字符串中的多个值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">swap</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'Tacos'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Burritos'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'great'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'fantastic'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Tacos are great!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Burritos are fantastic!</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-take" tabindex="-1"><a class="header-anchor" href="#str-take"><span><code v-pre>Str::take()</code></span></a></h4>
<p><code v-pre>Str::take</code> 方法返回字符串开头指定数量的字符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$taken</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">take</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Build something amazing!'</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Build</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-title" tabindex="-1"><a class="header-anchor" href="#str-title"><span><code v-pre>Str::title()</code></span></a></h4>
<p><code v-pre>Str::title</code> 方法将给定的字符串转换为<code v-pre>Title Case</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">title</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'a nice title uses the correct case'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// A Nice Title Uses The Correct Case</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-tobase64" tabindex="-1"><a class="header-anchor" href="#str-tobase64"><span><code v-pre>Str::toBase64()</code></span></a></h4>
<p><code v-pre>Str::toBase64</code> 方法将给定的字符串转换为 Base64 编码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$base64</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">toBase64</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// TGFyYXZlbA==</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-tohtmlstring" tabindex="-1"><a class="header-anchor" href="#str-tohtmlstring"><span><code v-pre>Str::toHtmlString()</code></span></a></h4>
<p><code v-pre>Str::toHtmlString</code> 方法将字符串实例转换为的实例 <code v-pre>Illuminate\Support\HtmlString</code>，可以在 Blade 模板中显示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$htmlString</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Nuno Maduro'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">toHtmlString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-trim" tabindex="-1"><a class="header-anchor" href="#str-trim"><span><code v-pre>Str::trim()</code></span></a></h4>
<p><code v-pre>Str::trim</code> 方法从给定字符串的开头和结尾删除空格（或其他字符）。与 PHP 的原生的 <code v-pre>trim</code> 函数不同，该 <code v-pre>Str::trim</code> 方法还会删除 unicode 空格字符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token string single-quoted-string">' foo bar '</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'foo bar'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-ltrim" tabindex="-1"><a class="header-anchor" href="#str-ltrim"><span><code v-pre>Str::ltrim()</code></span></a></h4>
<p><code v-pre>Str::ltrim</code> 方法从给定字符串的开头删除空格（或其他字符）。与 PHP 的原生的 <code v-pre>ltrim</code> 函数不同，该 <code v-pre>Str::ltrim</code> 方法还会删除 unicode 空格字符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">ltrim</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'  foo bar  '</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'foo bar  '</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-rtrim" tabindex="-1"><a class="header-anchor" href="#str-rtrim"><span><code v-pre>Str::rtrim()</code></span></a></h4>
<p><code v-pre>Str::rtrim</code> 方法从给定字符串的末尾删除空格（或其他字符）。与 PHP 原生的 <code v-pre>rtrim</code> 函数不同，该 <code v-pre>Str::rtrim</code> 方法还会删除 unicode 空格字符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">rtrim</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'  foo bar  '</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// '  foo bar'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-ucfirst" tabindex="-1"><a class="header-anchor" href="#str-ucfirst"><span><code v-pre>Str::ucfirst()</code></span></a></h4>
<p><code v-pre>Str::ucfirst</code> 方法返回首字母大写的给定字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">ucfirst</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Foo bar</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-ucsplit" tabindex="-1"><a class="header-anchor" href="#str-ucsplit"><span><code v-pre>Str::ucsplit()</code></span></a></h4>
<p><code v-pre>Str::ucsplit</code> 方法将给定的字符串按大写字符拆分为数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$segments</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">ucsplit</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'FooBar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// [0 => 'Foo', 1 => 'Bar']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-upper" tabindex="-1"><a class="header-anchor" href="#str-upper"><span><code v-pre>Str::upper()</code></span></a></h4>
<p><code v-pre>Str::upper</code> 方法将给定的字符串转换为大写：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">upper</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'laravel'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// LARAVEL</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-ulid" tabindex="-1"><a class="header-anchor" href="#str-ulid"><span><code v-pre>Str::ulid()</code></span></a></h4>
<p><code v-pre>Str::ulid</code> 方法生成一个 ULID，它是一个紧凑的、按时间排序的唯一标识符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword type-casting">string</span><span class="token punctuation">)</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">ulid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 01gd6r360bp37zj17nxb55yv40</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果您想要检索 <code v-pre>Illuminate\Support\Carbon</code> 表示创建给定 ULID 的日期和时间的日期实例，您可以使用 <code v-pre>createFromId</code> Laravel 的 Carbon 集成提供的方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Carbon</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$date</span> <span class="token operator">=</span> <span class="token class-name static-context">Carbon</span><span class="token operator">::</span><span class="token function">createFromId</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword type-casting">string</span><span class="token punctuation">)</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">ulid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在测试期间，「伪造」方法 <code v-pre>Str::ulid</code> 返回的值可能会很有用。为此，您可以使用 <code v-pre>createUlidsUsing</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Symfony<span class="token punctuation">\</span>Component<span class="token punctuation">\</span>Uid<span class="token punctuation">\</span>Ulid</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">createUlidsUsing</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Ulid</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'01HRDBNHHCKNW2AK4Z29SN82T9'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要指示 <code v-pre>ulid</code> 方法返回正常生成 ULID，您可以调用 <code v-pre>createUlidsNormally</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">createUlidsNormally</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="str-unwrap" tabindex="-1"><a class="header-anchor" href="#str-unwrap"><span><code v-pre>Str::unwrap()</code></span></a></h4>
<p><code v-pre>Str::unwrap</code> 方法从给定字符串的开头和结尾删除指定的字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">unwrap</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'-Laravel-'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'-'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Laravel</span></span>
<span class="line"></span>
<span class="line">    <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">unwrap</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'{framework: "Laravel"}'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'{'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'}'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// framework: "Laravel"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-uuid" tabindex="-1"><a class="header-anchor" href="#str-uuid"><span><code v-pre>Str::uuid()</code></span></a></h4>
<p>该<code v-pre>Str::uuid</code>方法生成一个 UUID（版本 4）：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword type-casting">string</span><span class="token punctuation">)</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">uuid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在测试期间，「伪造」 <code v-pre>Str::uuid</code> 方法返回的值可能会很有用。为此，您可以使用该 <code v-pre>createUuidsUsing</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Ramsey<span class="token punctuation">\</span>Uuid<span class="token punctuation">\</span>Uuid</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">createUuidsUsing</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token class-name static-context">Uuid</span><span class="token operator">::</span><span class="token function">fromString</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'eadbfeac-5258-45c2-bab7-ccb9b5ef74f9'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要指示该 <code v-pre>uuid</code> 方法返回正常生成 UUID，您可以调用该 <code v-pre>createUuidsNormally</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">createUuidsNormally</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="str-wordcount" tabindex="-1"><a class="header-anchor" href="#str-wordcount"><span><code v-pre>Str::wordCount()</code></span></a></h4>
<p><code v-pre>Str::wordCount</code> 方法返回字符串包含的单词数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">wordCount</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello, world!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 2</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-wordwrap" tabindex="-1"><a class="header-anchor" href="#str-wordwrap"><span><code v-pre>Str::wordWrap()</code></span></a></h4>
<p><code v-pre>Str::wordWrap</code> 方法将字符串包装为给定数量的字符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$text</span> <span class="token operator">=</span> <span class="token string double-quoted-string">"The quick brown fox jumped over the lazy dog."</span></span>
<span class="line"></span>
<span class="line">    <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">wordWrap</span><span class="token punctuation">(</span><span class="token variable">$text</span><span class="token punctuation">,</span> <span class="token argument-name">characters</span><span class="token punctuation">:</span> <span class="token number">20</span><span class="token punctuation">,</span> <span class="token keyword">break</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"&lt;br />\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">/*</span>
<span class="line">    The quick brown fox&lt;br /></span>
<span class="line">    jumped over the lazy&lt;br /></span>
<span class="line">    dog.</span>
<span class="line">    */</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-words" tabindex="-1"><a class="header-anchor" href="#str-words"><span><code v-pre>Str::words()</code></span></a></h4>
<p><code v-pre>Str::words</code> 限定一个字符串中的单词数量。一个额外的字符串可被传递给该方法的第三个参数，用来设定该被追加到被裁切字符串尾部的字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">words</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Perfectly balanced, as all things should be.'</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">' >>>'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Perfectly balanced, as >>></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-wrap" tabindex="-1"><a class="header-anchor" href="#str-wrap"><span><code v-pre>Str::wrap()</code></span></a></h4>
<p><code v-pre>Str::wrap</code> 方法用附加字符串或字符串对包装给定的字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">wrap</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'"'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// "Laravel"</span></span>
<span class="line"></span>
<span class="line">    <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">wrap</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'is'</span><span class="token punctuation">,</span> <span class="token argument-name">before</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'This '</span><span class="token punctuation">,</span> <span class="token argument-name">after</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">' Laravel!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// This is Laravel!</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str" tabindex="-1"><a class="header-anchor" href="#str"><span><code v-pre>str()</code></span></a></h4>
<p><code v-pre>str</code> 函数返回给定字符串的 <code v-pre>Illuminate\Support\Stringable</code> 实例。此函数相当于 <code v-pre>Str::of</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token function">str</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string single-quoted-string">' Otwell'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'Taylor Otwell'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果没有为 <code v-pre>str</code> 函数提供参数，则函数返回一个 <code v-pre>Illuminate\Support\Str</code> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token variable">$snake</span> <span class="token operator">=</span> <span class="token function">str</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">snake</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'FooBar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'foo_bar'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="trans" tabindex="-1"><a class="header-anchor" href="#trans"><span><code v-pre>trans()</code></span></a></h4>
<p><code v-pre>trans</code> 函数使用您的<a href="https://learnku.com/docs/laravel/11.x/localizationmd" target="_blank" rel="noopener noreferrer">语言包</a>翻译给定的翻译键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">echo</span> <span class="token function">trans</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.welcome'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果指定的翻译键不存在，则函数 <code v-pre>trans</code> 将返回给定的键。因此，使用上面的例子，如果翻译键不存在，则 <code v-pre>trans</code> 函数将返回 <code v-pre>messages.welcome</code>。</p>
<h4 id="trans-choice" tabindex="-1"><a class="header-anchor" href="#trans-choice"><span><code v-pre>trans_choice()</code></span></a></h4>
<p><code v-pre>trans_choice</code> 函数使用词形变化来翻译给定的翻译键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">echo</span> <span class="token function">trans_choice</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.notifications'</span><span class="token punctuation">,</span> <span class="token variable">$unreadCount</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果指定的翻译键不存在，则 <code v-pre>trans_choice</code> 函数将返回给定的键。因此，使用上面的例子，如果翻译键不存在，则<code v-pre>trans_choice</code>函数将返回 <code v-pre>messages.notifications</code>。</p>
<h2 id="流畅的字符串" tabindex="-1"><a class="header-anchor" href="#流畅的字符串"><span>流畅的字符串</span></a></h2>
<p>流畅的字符串提供了更流畅、面向对象的接口来处理字符串值，与传统字符串操作相比，它允许您使用更易读的语法使用多个字符串链式操作。</p>
<h4 id="after" tabindex="-1"><a class="header-anchor" href="#after"><span><code v-pre>after</code></span></a></h4>
<p><code v-pre>after</code> 方法返回字符串中给定值之后的所有内容。如果字符串中不存在该值，则返回整个字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">after</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ' my name'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="afterlast" tabindex="-1"><a class="header-anchor" href="#afterlast"><span><code v-pre>afterLast</code></span></a></h4>
<p><code v-pre>afterLast</code> 方法返回字符串中给定值最后一次出现后的所有内容。如果字符串中不存在该值，则返回整个字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'App\Http\Controllers\Controller'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">afterLast</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'\\'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'Controller'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="apa" tabindex="-1"><a class="header-anchor" href="#apa"><span><code v-pre>apa</code></span></a></h4>
<p><code v-pre>apa</code> 方法按照<a href="https://apastyle.apa.org/style-grammar-guidelines/capitalization/title-case" target="_blank" rel="noopener noreferrer">APA 指南</a>将给定的字符串转换为标题大小写：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'a nice title uses the correct case'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">apa</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// A Nice Title Uses the Correct Case</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="append" tabindex="-1"><a class="header-anchor" href="#append"><span><code v-pre>append</code></span></a></h4>
<p><code v-pre>append</code> 方法将给定的值附加到字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string single-quoted-string">' Otwell'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'Taylor Otwell'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="ascii" tabindex="-1"><a class="header-anchor" href="#ascii"><span><code v-pre>ascii</code></span></a></h4>
<p><code v-pre>ascii</code> 方法将尝试将字符串音译为 ASCII 值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'ü'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">ascii</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'u'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="basename" tabindex="-1"><a class="header-anchor" href="#basename"><span><code v-pre>basename</code></span></a></h4>
<p><code v-pre>basename</code> 方法将返回给定字符串的尾部名称部分：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/foo/bar/baz'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">basename</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'baz'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果需要，你可以提供一个将从尾部删除的「扩展名」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/foo/bar/baz.jpg'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">basename</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'baz'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="before" tabindex="-1"><a class="header-anchor" href="#before"><span><code v-pre>before</code></span></a></h4>
<p><code v-pre>before</code> 方法返回字符串中给定值之前的所有内容：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">before</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'my name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'This is '</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="beforelast" tabindex="-1"><a class="header-anchor" href="#beforelast"><span><code v-pre>beforeLast</code></span></a></h4>
<p><code v-pre>beforeLast</code> 方法返回字符串中给定值最后一次出现之前的所有内容：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">beforeLast</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'is'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'This '</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="between" tabindex="-1"><a class="header-anchor" href="#between"><span><code v-pre>between</code></span></a></h4>
<p><code v-pre>between</code> 方法返回两个值之间的字符串部分：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">between</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ' is my '</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="betweenfirst" tabindex="-1"><a class="header-anchor" href="#betweenfirst"><span><code v-pre>betweenFirst</code></span></a></h4>
<p><code v-pre>betweenFirst</code> 方法返回两个值之间的字符串的最小可能部分：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'[a] bc [d]'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">betweenFirst</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'['</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">']'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'a'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="camel" tabindex="-1"><a class="header-anchor" href="#camel"><span><code v-pre>camel</code></span></a></h4>
<p><code v-pre>camel</code> 方法将给定的字符串转换为 <code v-pre>camelCase</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo_bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">camel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'fooBar'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="charat" tabindex="-1"><a class="header-anchor" href="#charat"><span><code v-pre>charAt</code></span></a></h4>
<p><code v-pre>charAt</code> 方法返回指定索引处的字符。如果索引超出范围，则返回 <code v-pre>false</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$character</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name.'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">charAt</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 's'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="classbasename" tabindex="-1"><a class="header-anchor" href="#classbasename"><span><code v-pre>classBasename</code></span></a></h4>
<p><code v-pre>classBasename</code> 方法返回给定类的类名，并删除该类的命名空间：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$class</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Foo\Bar\Baz'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">classBasename</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'Baz'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="contains" tabindex="-1"><a class="header-anchor" href="#contains"><span><code v-pre>contains</code></span></a></h4>
<p><code v-pre>contains</code> 方法检测给定的字符串是否包含给定的值。此方法区分大小写：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$contains</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'my'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你还可以传递一个值数组来确定给定的字符串是否包含数组中的任意值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$contains</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'my'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="containsall" tabindex="-1"><a class="header-anchor" href="#containsall"><span><code v-pre>containsAll</code></span></a></h4>
<p><code v-pre>containsAll</code> 方法确定给定的字符串是否包含给定数组中的所有值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$containsAll</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">containsAll</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'my'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="dirname" tabindex="-1"><a class="header-anchor" href="#dirname"><span><code v-pre>dirname</code></span></a></h4>
<p><code v-pre>dirname</code> 方法返回给定字符串的上级目录部分：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/foo/bar/baz'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">dirname</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// '/foo/bar'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果有必要，您可以指定要从字符串中修剪的目录级别数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/foo/bar/baz'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">dirname</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// '/foo'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="excerpt" tabindex="-1"><a class="header-anchor" href="#excerpt"><span><code v-pre>excerpt</code></span></a></h4>
<p><code v-pre>excerpt</code> 方法从字符串中提取与该字符串中短语的第一个实例匹配的摘录：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$excerpt</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">excerpt</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'my'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'radius'</span> <span class="token operator">=></span> <span class="token number">3</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// '...is my na...'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>radius</code> 选项默认为 <code v-pre>100</code>，允许您定义截断字符串两边应出现的字符数。</p>
<p>此外，您还可以使用该<code v-pre>omission</code>选项来更改将被添加到和追加到截断字符串的字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$excerpt</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">excerpt</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'radius'</span> <span class="token operator">=></span> <span class="token number">3</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'omission'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'(...) '</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// '(...) my name'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="endswith" tabindex="-1"><a class="header-anchor" href="#endswith"><span><code v-pre>endsWith</code></span></a></h4>
<p><code v-pre>endsWith</code> 方法检测给定的字符串是否以给定的值结尾：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">endsWith</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你还可以传递一个值数组来检测给定的字符串是否以数组中的任意值结尾：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">endsWith</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">endsWith</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'this'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="exactly" tabindex="-1"><a class="header-anchor" href="#exactly"><span><code v-pre>exactly</code></span></a></h4>
<p><code v-pre>exactly</code> 方法检测给定的字符串是否与另一个字符串完全匹配：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">exactly</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="explode" tabindex="-1"><a class="header-anchor" href="#explode"><span><code v-pre>explode</code></span></a></h4>
<p><code v-pre>explode</code> 方法根据给定的分隔符拆分字符串，并返回包含拆分字符串各个部分的集合：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo bar baz'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">explode</span><span class="token punctuation">(</span><span class="token string single-quoted-string">' '</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// collect(['foo', 'bar', 'baz'])</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="finish" tabindex="-1"><a class="header-anchor" href="#finish"><span><code v-pre>finish</code></span></a></h4>
<p>如果字符串尚未以该值结尾，则 <code v-pre>finish</code> 方法将给定值的添加到该字符串中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$adjusted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'this/string'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">finish</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// this/string/</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$adjusted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'this/string/'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">finish</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// this/string/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="headline" tabindex="-1"><a class="header-anchor" href="#headline"><span><code v-pre>headline</code></span></a></h4>
<p><code v-pre>headline</code> 方法将把由大小写、连字符或下划线分隔的字符串转换为以空格分隔的字符串，并且每个单词的首字母大写：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$headline</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taylor_otwell'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">headline</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Taylor Otwell</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$headline</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'EmailNotificationSent'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">headline</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Email Notification Sent</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="inlinemarkdown" tabindex="-1"><a class="header-anchor" href="#inlinemarkdown"><span><code v-pre>inlineMarkdown</code></span></a></h4>
<p><code v-pre>inlineMarkdown</code> 方法使用<a href="https://commonmark.thephpleague.com/" target="_blank" rel="noopener noreferrer">CommonMark</a>将 GitHub 风格的 Markdown 转换为内联 HTML 。但是，与 <code v-pre>markdown</code> 方法不同，它不会将所有生成的 HTML 包装在块级元素中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$html</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'**Laravel**'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">inlineMarkdown</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// &lt;strong>Laravel&lt;/strong></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="markdown-安全-2" tabindex="-1"><a class="header-anchor" href="#markdown-安全-2"><span>Markdown 安全</span></a></h4>
<p>默认情况下，Markdown 支持原始 HTML，当与原始用户输入一起使用时，它将暴露跨站点脚本（XSS）漏洞。根据 <a href="https://commonmark.thephpleague.com/security/" target="_blank" rel="noopener noreferrer">CommonMark Security 文档</a>，你可以使用 <code v-pre>html_input</code> 选项转义或删除原始 HTML，以及 <code v-pre>allow_unsafe_links</code> 选项指定是否允许不安全链接。如果你需要允许某些原始 HTML，你应该将编译后的 Markdown 传递给 HTML 净化器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Inject: &lt;script>alert("Hello XSS!");&lt;/script>'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">inlineMarkdown</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'html_input'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'strip'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'allow_unsafe_links'</span> <span class="token operator">=></span> <span class="token constant boolean">false</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Inject: alert(&amp;quot;Hello XSS!&amp;quot;);</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="is" tabindex="-1"><a class="header-anchor" href="#is"><span><code v-pre>is</code></span></a></h4>
<p><code v-pre>is</code> 方法检测给定的字符串是否与给定的模式匹配。星号可以用作通配符值</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$matches</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foobar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">is</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo*'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$matches</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foobar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">is</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'baz*'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="isascii" tabindex="-1"><a class="header-anchor" href="#isascii"><span><code v-pre>isAscii</code></span></a></h4>
<p><code v-pre>isAscii</code> 方法检测给定的字符串是否是 ASCII 字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isAscii</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'ü'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isAscii</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="isempty" tabindex="-1"><a class="header-anchor" href="#isempty"><span><code v-pre>isEmpty</code></span></a></h4>
<p><code v-pre>isEmpty</code> 方法检测给定的字符串是否为空：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'  '</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="isnotempty" tabindex="-1"><a class="header-anchor" href="#isnotempty"><span><code v-pre>isNotEmpty</code></span></a></h4>
<p><code v-pre>isNotEmpty</code> 方法检测给定的字符串是否不为空：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'  '</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isNotEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// false</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isNotEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="isjson" tabindex="-1"><a class="header-anchor" href="#isjson"><span><code v-pre>isJson</code></span></a></h4>
<p><code v-pre>isJson</code> 方法检测给定的字符串是否为有效的 JSON：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'[1,2,3]'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isJson</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'{"first": "John", "last": "Doe"}'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isJson</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'{first: "John", last: "Doe"}'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isJson</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="isulid" tabindex="-1"><a class="header-anchor" href="#isulid"><span><code v-pre>isUlid</code></span></a></h4>
<p><code v-pre>isUlid</code> 方法检测给定的字符串是否是 ULID：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'01gd6r360bp37zj17nxb55yv40'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isUlid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isUlid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="isurl" tabindex="-1"><a class="header-anchor" href="#isurl"><span><code v-pre>isUrl</code></span></a></h4>
<p><code v-pre>isUrl</code> 方法检测给定的字符串是否是 URL：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://example.com'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isUrl</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isUrl</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>isUrl</code> 方法将多种协议视为有效。但是，您可以通过将协议提供给该 <code v-pre>isUrl</code> 方法来指定应被视为有效的协议：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://example.com'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isUrl</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'http'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'https'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="isuuid" tabindex="-1"><a class="header-anchor" href="#isuuid"><span><code v-pre>isUuid</code></span></a></h4>
<p><code v-pre>isUuid</code> 方法检测给定的字符串是否是 UUID：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'5ace9ab9-e9cf-4ec6-a19d-5881212a452c'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isUuid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isUuid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="kebab" tabindex="-1"><a class="header-anchor" href="#kebab"><span><code v-pre>kebab</code></span></a></h4>
<p><code v-pre>kebab</code> 方法将给定的字符串转换为<code v-pre>kebab-case</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'fooBar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">kebab</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// foo-bar</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="lcfirst" tabindex="-1"><a class="header-anchor" href="#lcfirst"><span><code v-pre>lcfirst</code></span></a></h4>
<p><code v-pre>lcfirst</code> 方法返回第一个字符小写的给定字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Foo Bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">lcfirst</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// foo Bar</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="length" tabindex="-1"><a class="header-anchor" href="#length"><span><code v-pre>length</code></span></a></h4>
<p><code v-pre>length</code> 方法返回给定字符串的长度：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$length</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 7</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="limit" tabindex="-1"><a class="header-anchor" href="#limit"><span><code v-pre>limit</code></span></a></h4>
<p><code v-pre>limit</code> 方法将给定的字符串截断为指定的长度：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$truncated</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'The quick brown fox jumps over the lazy dog'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// The quick brown fox...</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你还可以传递第二个参数来更改将附加到截断字符串末尾的字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$truncated</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'The quick brown fox jumps over the lazy dog'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">' (...)'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// The quick brown fox (...)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="lower" tabindex="-1"><a class="header-anchor" href="#lower"><span><code v-pre>lower</code></span></a></h4>
<p><code v-pre>lower</code> 方法将给定的字符串转换为小写：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'LARAVEL'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">lower</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'laravel'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="markdown" tabindex="-1"><a class="header-anchor" href="#markdown"><span><code v-pre>markdown</code></span></a></h4>
<p><code v-pre>markdown</code> 方法将 GitHub 风格的 Markdown 代码转换为 HTML 代码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$html</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'# Laravel'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">markdown</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// &lt;h1>Laravel&lt;/h1></span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$html</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'# Taylor &lt;b>Otwell&lt;/b>'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">markdown</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'html_input'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'strip'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// &lt;h1>Taylor Otwell&lt;/h1></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="markdown-安全-3" tabindex="-1"><a class="header-anchor" href="#markdown-安全-3"><span>Markdown 安全</span></a></h4>
<p>默认情况下，Markdown 支持原始 HTML，当与原始用户输入一起使用时，它将暴露跨站点脚本（XSS）漏洞。根据 <a href="https://commonmark.thephpleague.com/security/" target="_blank" rel="noopener noreferrer">CommonMark Security 文档</a>，你可以使用 <code v-pre>html_input</code> 选项转义或删除原始 HTML，以及 <code v-pre>allow_unsafe_links</code> 选项指定是否允许不安全链接。如果你需要允许某些原始 HTML，你应该将编译后的 Markdown 传递给 HTML 净化器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Inject: &lt;script>alert("Hello XSS!");&lt;/script>'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">markdown</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'html_input'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'strip'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'allow_unsafe_links'</span> <span class="token operator">=></span> <span class="token constant boolean">false</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// &lt;p>Inject: alert(&amp;quot;Hello XSS!&amp;quot;);&lt;/p></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="mask" tabindex="-1"><a class="header-anchor" href="#mask"><span><code v-pre>mask</code></span></a></h4>
<p><code v-pre>mask</code> 方法用重复字符掩盖字符串的一部分，可用于混淆电子邮件地址和电话号码等字符串片段：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taylor@example.com'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">mask</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'*'</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// tay***************</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果需要，你可以提供负数作为 <code v-pre>mask</code> 方法的第三或第四个参数，这将指示该方法从距字符串末尾的给定距离开始屏蔽：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taylor@example.com'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">mask</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'*'</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">15</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// tay***@example.com</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taylor@example.com'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">mask</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'*'</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// tayl**********.com</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="match" tabindex="-1"><a class="header-anchor" href="#match"><span><code v-pre>match</code></span></a></h4>
<p><code v-pre>match</code> 方法将返回与给定正则表达式模式匹配的字符串部分：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">match</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/bar/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'bar'</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">match</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/foo (.*)/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'bar'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="matchall" tabindex="-1"><a class="header-anchor" href="#matchall"><span><code v-pre>matchAll</code></span></a></h4>
<p><code v-pre>matchAll</code> 方法将返回一个包含与给定正则表达式模式匹配的字符串部分的集合：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bar foo bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">matchAll</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/bar/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// collect(['bar', 'bar'])</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果在表达式中指定匹配组，Laravel 将返回第一个匹配组的匹配项集合：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bar fun bar fly'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">matchAll</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/f(\w*)/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// collect(['un', 'ly']);</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果未找到匹配项，则将返回空集合。</p>
<h4 id="ismatch" tabindex="-1"><a class="header-anchor" href="#ismatch"><span><code v-pre>isMatch</code></span></a></h4>
<p>如果字符串与给定的正则表达式匹配，<code v-pre>isMatch</code> 方法将返回 <code v-pre>true</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isMatch</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/foo (.*)/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'laravel'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isMatch</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/foo (.*)/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="newline" tabindex="-1"><a class="header-anchor" href="#newline"><span><code v-pre>newLine</code></span></a></h4>
<p><code v-pre>newLine</code> 方法将「行尾」字符附加到字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$padded</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">newLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Framework'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'Laravel</span></span>
<span class="line">    <span class="token comment">//  Framework'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="padboth" tabindex="-1"><a class="header-anchor" href="#padboth"><span><code v-pre>padBoth</code></span></a></h4>
<p><code v-pre>padBoth</code> 方法包装了 PHP 的 <code v-pre>str_pad</code> 函数，用另一个字符串填充字符串的两侧，直到最终字符串达到所需的长度：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$padded</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'James'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">padBoth</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'_'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// '__James___'</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$padded</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'James'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">padBoth</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// '  James   '</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="padleft" tabindex="-1"><a class="header-anchor" href="#padleft"><span><code v-pre>padLeft</code></span></a></h4>
<p><code v-pre>padLeft</code> 方法包装了 PHP 的 <code v-pre>str_pad</code> 函数，用另一个字符串填充字符串的左侧，直到最终字符串达到所需的长度：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$padded</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'James'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">padLeft</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'-='</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// '-=-=-James'</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$padded</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'James'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">padLeft</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// '     James'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="padright" tabindex="-1"><a class="header-anchor" href="#padright"><span><code v-pre>padRight</code></span></a></h4>
<p><code v-pre>padRight</code> 方法包装了 PHP 的 <code v-pre>str_pad</code> 函数，用另一个字符串填充字符串的右侧，直到最终字符串达到所需的长度：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$padded</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'James'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">padRight</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'-'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'James-----'</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$padded</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'James'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">padRight</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'James     '</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="pipe" tabindex="-1"><a class="header-anchor" href="#pipe"><span><code v-pre>pipe</code></span></a></h4>
<p><code v-pre>pipe</code> 方法允许你通过将其当前值传递给给定的回调函数来转换字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$hash</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">pipe</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'md5'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">prepend</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Checksum: '</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'Checksum: a5c95b86291ea299fcbe64458ed12702'</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$closure</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">pipe</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$str</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'bar'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="plural" tabindex="-1"><a class="header-anchor" href="#plural"><span><code v-pre>plural</code></span></a></h4>
<p><code v-pre>plural</code> 方法将单数字符串转换为复数形式。此函数支持 <a href="https://learnku.com/docs/laravel/11.x/localizationmd#pluralization-language" target="_blank" rel="noopener noreferrer">Laravel 复数转换器支持的任何语言</a>)：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'car'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">plural</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// cars</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'child'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">plural</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// children</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以向函数提供一个整数作为第二个参数来检索字符串的单数或复数形式：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'child'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">plural</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// children</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'child'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">plural</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// child</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="position" tabindex="-1"><a class="header-anchor" href="#position"><span><code v-pre>position</code></span></a></h4>
<p><code v-pre>position</code> 方法返回字符串中子字符串第一次出现的位置。如果子字符串在字符串中不存在，则返回 <code v-pre>false</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$position</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello, World!'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">position</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 0</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$position</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello, World!'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">position</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'W'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 7</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="prepend" tabindex="-1"><a class="header-anchor" href="#prepend"><span><code v-pre>prepend</code></span></a></h4>
<p><code v-pre>prepend</code> 方法将给定的值添加到字符串的前面：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Framework'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">prepend</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel '</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Laravel Framework</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="remove" tabindex="-1"><a class="header-anchor" href="#remove"><span><code v-pre>remove</code></span></a></h4>
<p><code v-pre>remove</code> 方法从字符串中删除给定的值或数组中的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Arkansas is quite beautiful!'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'quite'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Arkansas is beautiful!</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>您还可以传递 <code v-pre>false</code> 给第二个参数来在删除字符串时忽略大小写。</p>
<h4 id="repeat" tabindex="-1"><a class="header-anchor" href="#repeat"><span><code v-pre>repeat</code></span></a></h4>
<p>该<code v-pre>repeat</code>方法重复给定的字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$repeated</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'a'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">repeat</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// aaaaa</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="replace" tabindex="-1"><a class="header-anchor" href="#replace"><span><code v-pre>replace</code></span></a></h4>
<p><code v-pre>replace</code> 方法替换字符串中的给定字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel 6.x'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'6.x'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'7.x'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Laravel 7.x</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>replace</code> 方法还接受一个 <code v-pre>caseSensitive</code> 参数。默认情况下，该<code v-pre>replace</code>方法区分大小写：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'macOS 13.x'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">replace</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token string single-quoted-string">'macOS'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'iOS'</span><span class="token punctuation">,</span> <span class="token argument-name">caseSensitive</span><span class="token punctuation">:</span> <span class="token constant boolean">false</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="replacearray" tabindex="-1"><a class="header-anchor" href="#replacearray"><span><code v-pre>replaceArray</code></span></a></h4>
<p><code v-pre>replaceArray</code> 方法使用数组按顺序替换字符串中的给定值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'The event will take place between ? and ?'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token variable">$string</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">replaceArray</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'?'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'8:30'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'9:00'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// The event will take place between 8:30 and 9:00</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="replacefirst" tabindex="-1"><a class="header-anchor" href="#replacefirst"><span><code v-pre>replaceFirst</code></span></a></h4>
<p><code v-pre>replaceFirst</code> 方法替换字符串中第一次出现的给定值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'the quick brown fox jumps over the lazy dog'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">replaceFirst</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'the'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'a'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// a quick brown fox jumps over the lazy dog</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="replacelast" tabindex="-1"><a class="header-anchor" href="#replacelast"><span><code v-pre>replaceLast</code></span></a></h4>
<p><code v-pre>replaceLast</code> 方法替换字符串中最后一次出现的给定值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'the quick brown fox jumps over the lazy dog'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">replaceLast</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'the'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'a'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// the quick brown fox jumps over a lazy dog</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="replacematches" tabindex="-1"><a class="header-anchor" href="#replacematches"><span><code v-pre>replaceMatches</code></span></a></h4>
<p><code v-pre>replaceMatches</code> 方法用给定的替换字符串替换与模式匹配的字符串的所有部分：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'(+1) 501-555-1000'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">replaceMatches</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/[^A-Za-z0-9]++/'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">''</span><span class="token punctuation">)</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// '15015551000'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>replaceMatches</code> 方法还能接受一个闭包参数，该闭包将使用与给定模式匹配的字符串的每个部分进行调用，从而允许您在闭包内执行替换逻辑并返回替换的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'123'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">replaceMatches</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/\d/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">array</span> <span class="token variable">$matches</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token string single-quoted-string">'['</span><span class="token operator">.</span><span class="token variable">$matches</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token operator">.</span><span class="token string single-quoted-string">']'</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// '[1][2][3]'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="replacestart" tabindex="-1"><a class="header-anchor" href="#replacestart"><span><code v-pre>replaceStart</code></span></a></h4>
<p><code v-pre>replaceStart</code> 仅当给定值出现在字符串的开头时，该方法才会替换该值的第一次出现的位置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">replaceStart</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Laravel World</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">replaceStart</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'World'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Hello World</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="replaceend" tabindex="-1"><a class="header-anchor" href="#replaceend"><span><code v-pre>replaceEnd</code></span></a></h4>
<p><code v-pre>replaceEnd</code> 仅当给定值出现在字符串末尾时，该方法才会替换该值的最后一次出现的位置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">replaceEnd</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'World'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Hello Laravel</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">replaceEnd</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Hello World</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="scan" tabindex="-1"><a class="header-anchor" href="#scan"><span><code v-pre>scan</code></span></a></h4>
<p><code v-pre>scan</code> 方法根据 <a href="https://www.php.net/manual/en/function.sscanf.php" target="_blank" rel="noopener noreferrer">PHP 函数 <code v-pre>sscanf</code></a>支持的格式将字符串输入解析为集合：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'filename.jpg'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">scan</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'%[^.].%s'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// collect(['filename', 'jpg'])</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="singular" tabindex="-1"><a class="header-anchor" href="#singular"><span><code v-pre>singular</code></span></a></h4>
<p><code v-pre>singular</code> 方法将字符串转换为单数形式。此函数支持 <a href="https://learnku.com/docs/laravel/11.x/localizationmd#pluralization-language" target="_blank" rel="noopener noreferrer">Laravel 复数转换器支持的任何语言</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$singular</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'cars'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">singular</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// car</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$singular</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'children'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">singular</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// child</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="slug" tabindex="-1"><a class="header-anchor" href="#slug"><span><code v-pre>slug</code></span></a></h4>
<p><code v-pre>slug</code> 方法根据给定的字符串生成 URL 友好的「slug」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$slug</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel Framework'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">slug</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'-'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// laravel-framework</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="snake" tabindex="-1"><a class="header-anchor" href="#snake"><span><code v-pre>snake</code></span></a></h4>
<p><code v-pre>snake</code> 方法将给定的字符串转换为<code v-pre>snake_case</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'fooBar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">snake</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// foo_bar</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="split" tabindex="-1"><a class="header-anchor" href="#split"><span><code v-pre>split</code></span></a></h4>
<p><code v-pre>split</code> 方法使用正则表达式将字符串拆分为集合：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$segments</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'one, two, three'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/[\s,]+/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// collect(["one", "two", "three"])</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="squish" tabindex="-1"><a class="header-anchor" href="#squish"><span><code v-pre>squish</code></span></a></h4>
<p><code v-pre>squish</code> 方法从字符串中删除所有多余的空格，包括单词之间的多余的空格：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'    laravel    framework    '</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">squish</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// laravel framework</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="start" tabindex="-1"><a class="header-anchor" href="#start"><span><code v-pre>start</code></span></a></h4>
<p>如果字符串尚未以该值开头，则 <code v-pre>start</code> 方法将给定值的单个实例添加到字符串中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$adjusted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'this/string'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">start</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// /this/string</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$adjusted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/this/string'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">start</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// /this/string</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="startswith" tabindex="-1"><a class="header-anchor" href="#startswith"><span><code v-pre>startsWith</code></span></a></h4>
<p>该<code v-pre>startsWith</code>方法检测给定的字符串是否以给定的值开头：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">startsWith</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="striptags" tabindex="-1"><a class="header-anchor" href="#striptags"><span><code v-pre>stripTags</code></span></a></h4>
<p><code v-pre>stripTags</code> 方法从字符串中删除所有 HTML 和 PHP 标签：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'&lt;a href="https://laravel.com">Taylor &lt;b>Otwell&lt;/b>&lt;/a>'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">stripTags</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Taylor Otwell</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'&lt;a href="https://laravel.com">Taylor &lt;b>Otwell&lt;/b>&lt;/a>'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">stripTags</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'&lt;b>'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Taylor &lt;b>Otwell&lt;/b></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="studly" tabindex="-1"><a class="header-anchor" href="#studly"><span><code v-pre>studly</code></span></a></h4>
<p><code v-pre>studly</code> 方法将给定的字符串转换为<code v-pre>StudlyCase</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo_bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">studly</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// FooBar</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="substr" tabindex="-1"><a class="header-anchor" href="#substr"><span><code v-pre>substr</code></span></a></h4>
<p><code v-pre>substr</code> 方法返回由给定的起始和长度参数指定的字符串部分：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel Framework'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Framework</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel Framework'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Frame</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="substrreplace" tabindex="-1"><a class="header-anchor" href="#substrreplace"><span><code v-pre>substrReplace</code></span></a></h4>
<p><code v-pre>substrReplace</code> 方法替换字符串中某一部分内的文本，从第二个参数指定的位置开始，替换第三个参数指定的字符数。传递 <code v-pre>0</code> 给该方法的第三个参数将在指定位置插入字符串，但不替换字符串中的任何现有字符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'1300'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">substrReplace</span><span class="token punctuation">(</span><span class="token string single-quoted-string">':'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 13:</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'The Framework'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">substrReplace</span><span class="token punctuation">(</span><span class="token string single-quoted-string">' Laravel'</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// The Laravel Framework</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="swap" tabindex="-1"><a class="header-anchor" href="#swap"><span><code v-pre>swap</code></span></a></h4>
<p><code v-pre>swap</code> 方法使用 PHP 的 <code v-pre>strtr</code> 函数替换字符串中的多个值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Tacos are great!'</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">swap</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'Tacos'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Burritos'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'great'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'fantastic'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Burritos are fantastic!</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="take" tabindex="-1"><a class="header-anchor" href="#take"><span><code v-pre>take</code></span></a></h4>
<p><code v-pre>take</code> 方法返回从字符串开头指定数量的字符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$taken</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Build something amazing!'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">take</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Build</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="tap" tabindex="-1"><a class="header-anchor" href="#tap"><span><code v-pre>tap</code></span></a></h4>
<p><code v-pre>tap</code> 方法将字符串传递给给定的闭包，允许您检查字符串并与之交互，而不会影响字符串本身。<code v-pre>tap</code>无论闭包返回什么，该方法都会返回原始字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string single-quoted-string">' Framework'</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">tap</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token function">dump</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'String after append: '</span><span class="token operator">.</span><span class="token variable">$string</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">upper</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// LARAVEL FRAMEWORK</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="test" tabindex="-1"><a class="header-anchor" href="#test"><span><code v-pre>test</code></span></a></h4>
<p><code v-pre>test</code> 方法检测字符串是否与给定的正则表达式模式匹配：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel Framework'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/Laravel/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="title" tabindex="-1"><a class="header-anchor" href="#title"><span><code v-pre>title</code></span></a></h4>
<p><code v-pre>title</code> 方法将给定的字符串转换为<code v-pre>Title Case</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'a nice title uses the correct case'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">title</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// A Nice Title Uses The Correct Case</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="tobase64" tabindex="-1"><a class="header-anchor" href="#tobase64"><span><code v-pre>toBase64()</code></span></a></h4>
<p><code v-pre>toBase64</code> 方法将给定的字符串转换为 Base64 编码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$base64</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">toBase64</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// TGFyYXZlbA==</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="trim" tabindex="-1"><a class="header-anchor" href="#trim"><span><code v-pre>trim</code></span></a></h4>
<p><code v-pre>trim</code> 方法修剪给定的字符串。与 PHP 的原生 <code v-pre>trim</code> 函数不同，Laravel 的 <code v-pre>trim</code> 方法还会删除 unicode 空格字符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'  Laravel  '</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'Laravel'</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/Laravel/'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'Laravel'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="ltrim" tabindex="-1"><a class="header-anchor" href="#ltrim"><span><code v-pre>ltrim</code></span></a></h4>
<p><code v-pre>ltrim</code> 方法修剪字符串的左侧。与 PHP 的原生 <code v-pre>ltrim</code> 函数不同，Laravel 的 <code v-pre>ltrim</code> 方法还会删除 unicode 空格字符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'  Laravel  '</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">ltrim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'Laravel  '</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/Laravel/'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">ltrim</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'Laravel/'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="rtrim" tabindex="-1"><a class="header-anchor" href="#rtrim"><span><code v-pre>rtrim</code></span></a></h4>
<p><code v-pre>rtrim</code> 方法修剪给定字符串的右侧。与 PHP 的原生 <code v-pre>rtrim</code> 函数不同，Laravel 的 <code v-pre>rtrim</code> 方法还会删除 unicode 空格字符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'  Laravel  '</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">rtrim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// '  Laravel'</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/Laravel/'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">rtrim</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// '/Laravel'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="ucfirst" tabindex="-1"><a class="header-anchor" href="#ucfirst"><span><code v-pre>ucfirst</code></span></a></h4>
<p><code v-pre>ucfirst</code> 方法返回首字母大写的给定字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">ucfirst</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Foo bar</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="ucsplit" tabindex="-1"><a class="header-anchor" href="#ucsplit"><span><code v-pre>ucsplit</code></span></a></h4>
<p><code v-pre>ucsplit</code> 方法将给定的字符串按大写字符拆分为集合：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Foo Bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">ucsplit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// collect(['Foo', 'Bar'])</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="unwrap" tabindex="-1"><a class="header-anchor" href="#unwrap"><span><code v-pre>unwrap</code></span></a></h4>
<p><code v-pre>unwrap</code> 方法从给定字符串的开头和结尾删除指定的字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'-Laravel-'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">unwrap</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'-'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Laravel</span></span>
<span class="line"></span>
<span class="line">    <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'{framework: "Laravel"}'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">unwrap</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'{'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'}'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// framework: "Laravel"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="upper" tabindex="-1"><a class="header-anchor" href="#upper"><span><code v-pre>upper</code></span></a></h4>
<p><code v-pre>upper</code> 方法将给定的字符串转换为大写：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$adjusted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'laravel'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">upper</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// LARAVEL</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="when" tabindex="-1"><a class="header-anchor" href="#when"><span><code v-pre>when</code></span></a></h4>
<p>如果给定条件为 <code v-pre>true</code>，则 <code v-pre>when</code> 方法调用给定的闭包。闭包将接收流畅的字符串实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token operator">-></span><span class="token function">when</span><span class="token punctuation">(</span><span class="token constant boolean">true</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                        <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string single-quoted-string">' Otwell'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'Taylor Otwell'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果需要，你可以将另一个闭包作为第三个参数传递给 <code v-pre>when</code> 方法。如果条件参数的结果为 <code v-pre>false</code> ，将执行此闭包。</p>
<h4 id="whencontains" tabindex="-1"><a class="header-anchor" href="#whencontains"><span><code v-pre>whenContains</code></span></a></h4>
<p>如果字符串包含给定值，则 <code v-pre>whenContains</code> 方法调用给定的闭包。闭包将接收流畅的字符串实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'tony stark'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">whenContains</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'tony'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                    <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">title</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'Tony Stark'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果需要，你可以将另一个闭包作为第三个参数传递给 <code v-pre>when</code> 方法。如果字符串不包含给定值，则执行此闭包。</p>
<blockquote>
<p><strong>译者注：</strong> 原文在手册中应该是写错了，正确的应该是「你可以将另一个闭包作为第三个参数传递给 <code v-pre>whenContains</code> 方法」，待手册更新后更改以上内容。</p>
</blockquote>
<p>您还可以传递一个值数组来检测给定的字符串是否包含数组中的任意值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'tony stark'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">whenContains</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'tony'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'hulk'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                    <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">title</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Tony Stark</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="whencontainsall" tabindex="-1"><a class="header-anchor" href="#whencontainsall"><span><code v-pre>whenContainsAll</code></span></a></h4>
<p>如果字符串包含所有给定的子字符串，则 <code v-pre>whenContainsAll</code> 方法将调用给定的闭包。闭包将接收流畅的字符串实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'tony stark'</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token operator">-></span><span class="token function">whenContainsAll</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'tony'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'stark'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                        <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">title</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'Tony Stark'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果需要，你可以将另一个闭包作为第三个参数传递给该 <code v-pre>when</code> 方法。如果条件参数的结果为 <code v-pre>false</code> ，将执行此闭包。</p>
<blockquote>
<p><strong>译者注：</strong> 原文在手册中应该是写错了，正确的应该是「你可以将另一个闭包作为第三个参数传递给 <code v-pre>whenContainsAll</code> 方法。如果字符串不包含所有给定的子字符串，将执行此闭包。」，待手册更新后更改以上内容。</p>
<h4 id="whenempty" tabindex="-1"><a class="header-anchor" href="#whenempty"><span><code v-pre>whenEmpty</code></span></a></h4>
</blockquote>
<p>如果字符串为空，则 <code v-pre>whenEmpty</code> 方法将调用给定的闭包。如果闭包返回一个值，<code v-pre>whenEmpty</code> 方法也将返回该值。如果闭包不返回值，则将返回流畅的字符串实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'  '</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whenEmpty</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">prepend</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'Laravel'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="whennotempty" tabindex="-1"><a class="header-anchor" href="#whennotempty"><span><code v-pre>whenNotEmpty</code></span></a></h4>
<p>如果字符串不为空，则 <code v-pre>whenNotEmpty</code> 方法将调用给定的闭包。如果闭包返回一个值，<code v-pre>whenNotEmpty</code> 方法也将返回该值。如果闭包不返回值，则将返回流畅的字符串实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Framework'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whenNotEmpty</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">prepend</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel '</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'Laravel Framework'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="whenstartswith" tabindex="-1"><a class="header-anchor" href="#whenstartswith"><span><code v-pre>whenStartsWith</code></span></a></h4>
<p>如果字符串以给定的子字符串开头，则 <code v-pre>whenStartsWith</code> 方法调用给定的闭包。闭包将接收流畅的字符串实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'disney world'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whenStartsWith</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'disney'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">title</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'Disney World'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="whenendswith" tabindex="-1"><a class="header-anchor" href="#whenendswith"><span><code v-pre>whenEndsWith</code></span></a></h4>
<p>如果字符串以给定的子字符串结尾，则 <code v-pre>whenEndsWith</code> 方法调用给定的闭包。闭包将接收流畅的字符串实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'disney world'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whenEndsWith</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'world'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">title</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'Disney World'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="whenexactly" tabindex="-1"><a class="header-anchor" href="#whenexactly"><span><code v-pre>whenExactly</code></span></a></h4>
<p>如果字符串与给定的字符串完全匹配，则 <code v-pre>whenExactly</code> 方法将调用给定的闭包。闭包将接收流畅的字符串实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'laravel'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whenExactly</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'laravel'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">title</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'Laravel'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="whennotexactly" tabindex="-1"><a class="header-anchor" href="#whennotexactly"><span><code v-pre>whenNotExactly</code></span></a></h4>
<p>如果字符串与给定的字符串不完全匹配，则 <code v-pre>whenNotExactly</code> 方法调用给定的闭包。闭包将接收流畅的字符串实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'framework'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whenNotExactly</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'laravel'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">title</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'Framework'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="whenis" tabindex="-1"><a class="header-anchor" href="#whenis"><span><code v-pre>whenIs</code></span></a></h4>
<p>如果字符串与给定模式匹配，则 <code v-pre>whenIs</code> 方法将调用给定的闭包。星号可用作通配符值。闭包将接收流畅的字符串实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo/bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whenIs</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo/*'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/baz'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'foo/bar/baz'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="whenisascii" tabindex="-1"><a class="header-anchor" href="#whenisascii"><span><code v-pre>whenIsAscii</code></span></a></h4>
<p>如果字符串是 7 位 ASCII，则 <code v-pre>whenIsAscii</code> 方法将调用给定的闭包。闭包将接收流畅的字符串实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'laravel'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whenIsAscii</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">title</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'Laravel'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="whenisulid" tabindex="-1"><a class="header-anchor" href="#whenisulid"><span><code v-pre>whenIsUlid</code></span></a></h4>
<p>如果字符串是有效的 ULID，则 <code v-pre>whenIsUlid</code> 方法将调用给定的闭包。闭包将接收流畅的字符串实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'01gd6r360bp37zj17nxb55yv40'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whenIsUlid</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// '01gd6r36'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="whenisuuid" tabindex="-1"><a class="header-anchor" href="#whenisuuid"><span><code v-pre>whenIsUuid</code></span></a></h4>
<p>如果字符串是有效的 UUID，则 <code v-pre>whenIsUuid</code> 方法将调用给定的闭包。闭包将接收流畅的字符串实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'a0a2a2d2-0b87-4a18-83f2-2529882be2de'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whenIsUuid</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'a0a2a2d2'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="whentest" tabindex="-1"><a class="header-anchor" href="#whentest"><span><code v-pre>whenTest</code></span></a></h4>
<p>如果字符串与给定的正则表达式匹配，则 <code v-pre>whenTest</code> 方法将调用给定的闭包。闭包将接收流畅的字符串实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'laravel framework'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whenTest</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/laravel/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">title</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 'Laravel Framework'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="wordcount" tabindex="-1"><a class="header-anchor" href="#wordcount"><span><code v-pre>wordCount</code></span></a></h4>
<p><code v-pre>wordCount</code> 方法返回字符串包含的单词数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello, world!'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">wordCount</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 2</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="words" tabindex="-1"><a class="header-anchor" href="#words"><span><code v-pre>words</code></span></a></h4>
<p><code v-pre>words</code> 方法限制字符串中的单词数。如有必要，您可以指定将附加到截断字符串的附加字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Perfectly balanced, as all things should be.'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">words</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">' >>>'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Perfectly balanced, as >>></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/stringsmd/16688" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/st...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/stringsmd/16688" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/st...</a></p>
</blockquote>
</div></template>


